#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const inquirer = require("inquirer");
const Handlebars = require("handlebars");
const dest = "project";

inquirer
  .prompt([
    {
      type: "input",
      name: "firstname",
      message: "请输入你的名字",
    },
    {
      type: "input",
      name: "lastname",
      message: "请输入你的姓氏",
    },
  ])
  .then((anwsers) => {
    fs.readdir(path.resolve(__dirname, "../template"), (err, files) => {
      if (!err) {
        files.forEach((file) => {
          const data = fs.readFileSync(
            path.resolve(__dirname, `../template/${file}`)
          );
          const outputDir = path.resolve(process.cwd(), dest);
          if (!fs.existsSync(outputDir)) {
            fs.mkdirSync(outputDir);
          }
          const template = Handlebars.compile(data.toString());
          fs.writeFileSync(path.resolve(outputDir, file), template(anwsers));
        });
      } else {
        console.error(err);
      }
    });
  });
